unit EveCentralRetriever;

interface

uses
  Classes, SysUtils,

  XMLDoc, XMLIntf,

  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP;

type
  TEveCentralRetriever = class(TThread)
  private
  protected
    procedure Execute; override;
  public
    // input
    ParameterLists: TList;
    Version: String;

    // output
    TextResult: TStringList;
    Error: String;
    //Items, Regions: TStringList;
    Done: Boolean;
    ReceivedLists: Integer;
  end;

implementation

{ Important: Methods and properties of objects in visual components can only be
  used in a method called using Synchronize, for example,

      Synchronize(UpdateCaption);

  and UpdateCaption could look like,

    procedure TEveCentralRetriever.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }

{ TEveCentralRetriever }

procedure TEveCentralRetriever.Execute;
var
  i: Integer;
//  i, j, len: Integer;
//  smallitemslist: TStringList;
//  itemslistlist: array of TStringList;

  http: TIdHTTP;
  parameters: TStringList;
//  tempXMLresults: array of String;
//  tempXMLDoc: TXMLDocument;
//  node, tempitemsnode, itemsnode: IXMLNode;
begin
  TextResult := TStringList.Create;
  Done := false;
  ReceivedLists := 0;
  Error := '';

{  // split up items list in smaller lists of 20 items each, as EVE-Central only allows 20 items to be retrieved at once
  smallitemslist := TStringList.Create;
  for i := 0 to Items.Count -1 do begin
    if (smallitemslist.Count = 20) then begin
      // list is full. Add region information and create a new list
      for j := 0 to Regions.Count -1 do begin
        smallitemslist.Add(Regions[j]);
      end;
      len := Length(itemslistlist);
      SetLength(itemslistlist, len+1);
      itemslistlist[len] := smallitemslist;
      smallitemslist := TStringList.Create;
    end;
    smallitemslist.Add(Items[i]);
  end;
  // Also add the last few items after adding region information
  for j := 0 to Regions.Count -1 do begin
    smallitemslist.Add(Regions[j]);
  end;
  len := Length(itemslistlist);
  SetLength(itemslistlist, len + 1);
  itemslistlist[len] := smallitemslist;   }

  // retrieve prices from eve-central
  http := TIdHTTP.Create;
  http.Request.UserAgent := 'Invention Calculator v' + Version + ' (http://myeve.eve-online.com/ingameboard.asp?a=topic&threadID=743617) - Tool to calculate profit from inventing T2 items';
  http.ConnectTimeout := 60000; // timeout in milliseconds
//  SetLength(tempXMLresults, Length(itemslistlist));
  for i := 0 to ParameterLists.Count -1 do begin
//    showmessage(inttostr(itemslistlist[i].Count));
    try
      parameters := TStringList(ParameterLists[i]);
//      tempXMLresults[i] := http.Post('http://eve-central.com/api/marketstat', itemslistlist[i]);
      TextResult.Add(http.Post('http://eve-central.com/api/marketstat', parameters));
    except
      on e: Exception do begin
        Error := e.Message;
        break;
      end;
    end;
    if (Terminated) then begin
      Error := 'EveCentralRetriever thread terminated';
      exit;
    end;
    ReceivedLists := ReceivedLists + 1;
  end;
  http.Free;
  if (Error = '') then begin
    // merge all received results to one XML file
{    XMLresult := TXMLDocument.Create(nil);
    XMLresult.Active := true;
    XMLresult.LoadFromXML(tempXMLresults[0]);
    itemsnode := XMLresult.DocumentElement.ChildNodes[0]; // <marketstat> element
    for i := 1 to Length(tempXMLresults) -1 do begin
      tempXMLDoc := TXMLDocument.Create(nil);
      tempXMLDoc.Active := true;
      tempXMLDoc.LoadFromXML(tempXMLresults[i]);
      tempitemsnode := tempXMLDoc.DocumentElement.ChildNodes[0]; // <marketstat> element
      for j := 0 to tempitemsnode.ChildNodes.Count -1 do begin
        node := tempitemsnode.ChildNodes[j];
        itemsnode.ChildNodes.Add(node.CloneNode(true));
      end;
      tempXMLDoc.Free;
      if (Terminated) then begin
        Error := 'EveCentralRetriever thread terminated';
        exit;
      end;
    end;}
  end;
  Done := true;
end;

end.

